#include "message.h"
#include "net.h"
#include <thread>
#include "dispather.h"
void onRpcResponse(const rpc::BaseConnection::ptr &conn, const rpc::BaseMessage::ptr &msg)
{
    std::cout << "收到rpc响应" << std::endl;
    std::string body = msg->serialize();
    std::cout << body << std::endl;
}
void ontopicResponse(const rpc::BaseConnection::ptr &conn, const rpc::BaseMessage::ptr &msg)
{
    std::cout << "收到topic响应" << std::endl;
    std::string body = msg->serialize();
    std::cout << body << std::endl;
}
int main()
{
    auto dispather = std::make_shared<rpc::dispather>();
    dispather->registerHandler(lxprpc::MType::RSP_RPC, onRpcResponse);
    dispather->registerHandler(lxprpc::MType::RSP_TOPIC, ontopicResponse);
    auto client = rpc::ClientFactory::create("127.0.0.1", 9090);
    auto message_cb = std::bind(&rpc::dispather::onMessage, dispather.get(), std::placeholders::_1, std::placeholders::_2);

    client->setMessageCallback(message_cb);
    client->connect();
    auto rpc_req = rpc::MessageFactory::create<rpc::RpcRequest>();
    rpc_req->setrId("11111");
    rpc_req->setMtype(lxprpc::MType::REQ_RPC);
    rpc_req->Setmethod("add");
    Json::Value param;
    param["nums1"] = 11;
    param["nums2"] = 22;
    rpc_req->Setparams(param);
    client->send(rpc_req);

    auto topic_req = rpc::MessageFactory::create<rpc::TopicRequest>();
    topic_req->setrId("222222");
    topic_req->setMtype(lxprpc::MType::REQ_TOPIC);
    topic_req->Setoptype(lxprpc::TopicOptype::TOPIC_CREATE);
    topic_req->Settopic("news");
    client->send(topic_req);

    std::this_thread::sleep_for(std::chrono::seconds(10));
    client->shutdown();
    return 0;
}